/*

This file is part of Zephyr.

    Zephyr is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Zephyr is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Zephyr.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <mainwindow.h>
#include <ui_mainwindow.h>
#include <aboutwindow.h>
#include <createnewconnection.h>
#include <qdebug.h>
#include <openconnection.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    /*Map setup*/
    map = new LightMaps(this);
    ui->gridLayout->addWidget(map,0,0);
    map->setMinimumSize(400,400);
    map->show();
    connected = false;

    socket = new QTcpSocket(this);
    /*!
    *   Connect every need SIGNAL to require SLOT
    */

    connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(changeTextBrowsers(int)));
}

void MainWindow::receivedIncomingData(){
    /*Need to put retrieving of data from socket here.  This function will then pass data to parser.
      The parser will parse the incoming data, submit it for logging then determine if the user
      currently wants to see the data.  If so, the parser will pass the data along to update the text
      browsers and any other systems requiring the data.*/
    QString message = socket->readLine();
    ui->incomingTextBrowser->append(message);

    qDebug() << message;
}

MainWindow::~MainWindow()
{
    delete ui;
}

/*!
*   This function creates the about window and shows it.
*/
void MainWindow::on_actionAbout_Zephyr_triggered()
{
    aboutWin = new aboutWindow(this);
    aboutWin->setAttribute(Qt::WA_DeleteOnClose);
    aboutWin->show();
    map->setCenter(0,0);
}

/*!
*   This function creates the about window and shows it.
*/
void MainWindow::on_actionClear_Cache_triggered()
{
    map->m_normalMap->cache->clear();
}


/*!
 * This function hides the side panel
 */
void MainWindow::on_actionShow_Side_Panel_triggered()
{
    ui->connectionAndButtonsFrame->setHidden(!ui->connectionAndButtonsFrame->isHidden());
}



/*!
 * This function hides the buttons in the side panel
 */
void MainWindow::on_actionShow_Buttons_triggered()
{
    ui->buttonBox->setHidden(!ui->buttonBox->isHidden());
}

/*!
 * This function hides the data feed
 */
void MainWindow::on_actionShow_Data_Feed_triggered()
{
    ui->tabWidget->setHidden(!ui->tabWidget->isHidden());
}

void MainWindow::on_actionQuit_triggered()
{
    this->close();
}


/*!
* The following is a basic showing of how to use the text browsers in the tab widgets.  We should edit this function to allow the input
* of strings which can be sent from the received data functions.  These strings can then be sorted into whichever window is needed
*/
void MainWindow::changeTextBrowsers(int newTab){
    if(newTab==0){
        ui->incomingTextBrowser->setText("Incoming tab selected");
    }
    else if(newTab==1){
        ui->outgoingTextBrowser->setText("Outgoing tab selected");
    }
    else{
        qDebug() << "Error, invalid tab selected!";
    }
    curTab = newTab;
}

/*!
 * This function creates the new connection window and shows it
 */
void MainWindow::on_newConnection_clicked()
{
    newConnect = new createNewConnection(this);
    newConnect->setAttribute(Qt::WA_DeleteOnClose);
    connect(newConnect, SIGNAL(connectClicked(QString,QString,QString,QString,bool)), this, SLOT(connectButtonPressed(QString,QString,QString,QString,bool)));
    // shows the window
    newConnect->show();
}

/*!
 * This function creates the open connection window and shows it
 */
void MainWindow::on_openConnection_clicked()
{
    // creates a new instance of open connection window
    openConnect = new openConnection(this);
    // deletes the window on close
    openConnect->setAttribute(Qt::WA_DeleteOnClose);
    // shows the window
    openConnect->show();
}



void MainWindow::connectButtonPressed(QString username, QString pass, QString server, QString port, bool saveConnection){
/**

  ANYTHING BELOW THIS IS FOR CONNECTIONS AND THE DATA FROM THEM
  */

qDebug() << username << pass << server << port;

    if (connected == false)
    {
        socket = new QTcpSocket(this); // forgot this in the original code... not sure how necessary

        connect(socket, SIGNAL(readyRead()), this, SLOT(receivedIncomingData()));
        connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));

        socket->connectToHost(server, quint16(port.toInt()));

        QByteArray Authorization = "user " + username.toAscii() + " pass " + pass.toAscii() + " vers ZephyrAPRS beta";
        Authorization.append("\n");
        qDebug() << Authorization;
        socket->write(Authorization);
        /*this is code to append authorizations to send and verify*/


        if(socket->waitForConnected())
        {
            QString connec = "# Application Status:  Connected";
            ui->incomingTextBrowser->append(connec);
//            QFile aprsRecieved("aprsRecieved.ini");
//            aprsRecieved.open(QIODevice::ReadWrite);
//            QTextStream outputStream(&aprsRecieved);
//            outputStream << message << "\n" << endl;
//            aprsRecieved.close();
            newConnect->close();
            if(saveConnection == true){
                // creates saved connections file and outputs the connection data to it ... doesnt need to be here but for now no better place for it
                 QFile savedConnectionFile("savedConnections.ini");
                 savedConnectionFile.open(QIODevice::ReadWrite);
                 QTextStream outputStream(&savedConnectionFile);
                 outputStream << username << endl;
                 outputStream << pass << endl;
                 outputStream << server << endl;
                 outputStream << port << endl << endl;
                 savedConnectionFile.close();
            }
        }
        else
        {
             QString error = "# Application Status: Error -- unable to connect.\n"
                             "# Please verify server name and port number.\n";
             ui->incomingTextBrowser->append(error);
             QApplication::beep();
             newConnect->show();
        } // end of invalid connection
    }
}
<<<<<<< Updated upstream

void MainWindow::updateTextBrowser(){
    QByteArray message = socket->readLine();
    ui->incomingTextBrowser->append(message);
}
=======
>>>>>>> Stashed changes
